import Realm from "realm"

const userId = "ganSmall_1"
export const schemaName = {
    UserInfo: "UserInfo" + userId,
    UserInfoList: "UserInfoList" + userId,
    MsgSeq: "MsgSeq" + userId,
    GroupMsgSeq: "GroupMsgSeq" + userId,
    MsgLis: "MsgLis" + userId,
    GroupMsgLis: "GroupMsgLis" + userId,
    FriendList: "FriendList" + userId,
    GroupList: "GroupList" + userId,
    GroupInfo: "GroupInfo" + userId,
    GroupMembers: "GroupMembers" + userId,
    // GroupAdmins: "GroupAdmins" + userId,
    WalletList: "WalletList" + userId,
    CurrentWalle: "CurrentWalle" + userId,
    RpcUrlList: "RpcUrlList" + userId,
    CurrentRpcUrl: "CurrentRpcUrl" + userId,
    BrowserSearchLogs: "BrowserSearchLogs" + userId,
    BrowserPopularLogs: "BrowserPopularLogs" + userId,
}
export const schema = {
    schema: [
        {
            name: schemaName.UserInfo,
            primaryKey: 'cId',
            properties: {
                cId: { type: 'int', default: 1, optional: true },
                Id: "int",
                Address: "string",
                Followers: { type: 'int', default: 1, optional: true },
                FriendSeq: { type: 'int', default: 1, optional: true },
                NickName: { type: 'string', default: "My", optional: true },
                ProfilePhoto: { type: 'string', default: "", optional: true },
                Status: { type: 'int', default: 0, optional: true },
                Subscriptions: { type: 'int', default: 0, optional: true },
                Tweet: { type: 'string', default: "0", optional: true },
                USDT: { type: 'string', default: "0", optional: true },
                UserLevel: { type: 'int', default: 0, optional: true },
                Token: "string",
            }
        },
        {
            name: schemaName.UserInfoList,
            primaryKey: 'Id',
            properties: {
                Id: "int",
                Address: "string",
                Followers: { type: 'int', default: 1, optional: true },
                FriendSeq: { type: 'int', default: 1, optional: true },
                NickName: { type: 'string', default: "My", optional: true },
                ProfilePhoto: { type: 'string', default: "", optional: true },
                Status: { type: 'int', default: 0, optional: true },
                Subscriptions: { type: 'int', default: 0, optional: true },
                Tweet: { type: 'string', default: "0", optional: true },
                USDT: { type: 'string', default: "0", optional: true },
                UserLevel: { type: 'int', default: 0, optional: true },
                Token: "string",
            }
        },
        {
            name: schemaName.FriendMsgSeq,
            primaryKey: 'Id',
            properties: {
                Cur: 'int',
                Total: 'int',
                Id: { type: 'int', default: 1 },
                UserId: 'int',
            }
        },
        {
            name: schemaName.GroupMsgSeq,
            primaryKey: 'G',
            properties: {
                G: { type: 'int', default: 1 },
                Cur: 'int',
                Total: 'int',
                UserId: 'int',
            }
        },
        {
            name: schemaName.FriendMsgLis,
            primaryKey: '_id',
            properties: {
                F: 'int',//发送者的用户id
                M: { type: 'string', default: "" },//具体消息
                MT: 'int',//消息类型，
                S: { type: 'int', default: -1, optional: true },//消息id索引  -1 自己发送
                T: 'int',//发送消息的时间戳
                _id: { type: 'string', default: "0", optional: true }, //自定义key
                Unread: { type: 'int', default: 1, optional: true },//自定义未读
                SendState: { type: 'int', default: 0, optional: true },//自定义 0 正常 1 发送中 / 2发送失败 / 3已被删除
                UserId: 'int',//自定义 登录ID
                RedPacketId: { type: 'int', default: 0, optional: true },//自定义 红包Id
                RedPacketIdState: { type: 'int', default: 0, optional: true }//自定义 红包状态0未领取 1 已领取
            }
        },
        {
            name: schemaName.GroupMsgLis,
            primaryKey: '_id',
            properties: {
                G: 'int',//群id
                F: 'int',//发送者的用户id
                M: { type: 'string', default: "" },//具体消息
                MT: 'int',//消息类型，
                P: { type: 'string', default: "" },//头像
                R: { type: 'string', default: "" },//名称
                S: { type: 'int', default: -1, optional: true },//消息id索引  -1 自己发送
                T: 'int',//发送消息的时间戳
                _id: { type: 'string', default: "0", optional: true }, //自定义key
                Unread: { type: 'int', default: 1, optional: true },//自定义未读
                SendState: { type: 'int', default: 0, optional: true },//自定义 0 正常 1 发送中 / 2发送失败 / 3已被删除
                UserId: 'int',//自定义 登录ID
                RedPacketId: { type: 'int', default: 0, optional: true },//自定义 红包Id
                RedPacketIdState: { type: 'int', default: 0, optional: true }//自定义 红包状态0未领取 1 已领取
            }
        },
        {
            name: schemaName.FriendList,
            primaryKey: 'F',
            properties: {
                F: "int",
                P: "string",
                R: 'string',
                S: 'int',
                UserId: 'int',
                State: { type: 'int', default: 1, optional: true },//自定义 0 删除 1 正常
            }
        },
        {
            name: schemaName.GroupList,
            primaryKey: 'G',
            properties: {
                G: "int",//群id
                L: "string",//图片
                R: 'string',//名称
                S: 'int',//当前索引
                U: 'int',//总消息
                UserId: 'int',
                State: { type: 'int', default: 1, optional: true },//
            }
        },
        {
            name: schemaName.GroupInfo,
            primaryKey: 'Id',
            properties: {
                Address: { type: 'string', default: "" },//群主地址
                Admins: { type: 'string', default: "" },//
                CreatedAt: { type: 'string', default: "" },//
                GroupLevel: { type: 'int', default: 0, optional: true },//群等级
                GroupName: { type: 'string', default: "" },//具体消息
                GroupValue: { type: 'int', default: 0, optional: true },//群名称
                Id: { type: 'int', default: 0, optional: true },//群ID
                Logo: { type: 'string', default: "" },//群图片
                Notice: { type: 'string', default: "" },//群公告
                Num: { type: 'int', default: 0, optional: true },//群成员数量
                OwnerId: { type: 'int', default: 0, optional: true },//拥有者Id
                SortValue: { type: 'int', default: 0, optional: true },//
                Status: { type: 'int', default: 0, optional: true },//群状态
                UserId: 'int',
            }
        },
        {
            name: schemaName.GroupMembers,
            primaryKey: '_G_I',
            properties: {
                _G_I: "string",//群id-用户Id
                G: "int",//群id
                I: "int",//用户iD
                P: "string",//图片
                R: 'string',//名称
                S: 'int',//
                UserId: 'int',
            }
        },
        // {
        //     name: schemaName.GroupAdmins,
        //     primaryKey: 'G',
        //     properties: {
        //         G: "int",//群id
        //         L: "string",//图片
        //         R: 'string',//名称
        //         S: 'int',//当前索引
        //         U: 'int',//总消息
        //         UserId: 'int',
        //         State: { type: 'int', default: 1, optional: true },//
        //     }
        // },
        {
            name: schemaName.WalletList,
            primaryKey: 'id',
            properties: {
                id: { type: 'int', default: 1 },
                address: "string",
                keystore: "string",
                type: { type: 'int', default: 1 },//1 记组词 2key
                chainId: { type: 'int', default: 56 },
                name: { type: 'string', default: "BNB-1" },
                balance: { type: 'string', default: "0" },
                symbol: { type: 'string', default: "BNB" },
                decimals: { type: 'int', default: 18 },
                state: { type: 'int', default: 1 },//0删除 1正常
            }
        },
        {
            name: schemaName.CurrentWalle,
            primaryKey: 'cid',
            properties: {
                cid: { type: 'int', default: 1 },
                id: { type: 'int', default: 1 },
                address: "string",
                keystore: "string",
                type: { type: 'int', default: 1 },//1 记组词 2key
                chainId: { type: 'int', default: 56 },
                name: { type: 'string', default: "BNB-1" },
                balance: { type: 'string', default: "0" },
                symbol: { type: 'string', default: "BNB" },
                decimals: { type: 'int', default: 18 },
                state: { type: 'int', default: 1 },//0删除 1正常
            }
        },
        {
            name: schemaName.RpcUrlList,
            primaryKey: 'id',
            properties: {
                id: { type: 'int', default: 1 },
                chainId: { type: 'int', default: 1 },
                rpcUrlName: "string",
                rpcUrl: "string",
            }
        },
        {
            name: schemaName.CurrentRpcUrl,
            primaryKey: 'chainId',
            properties: {
                chainId: { type: 'int', default: 1 },
                rpcUrlName: "string",
                rpcUrl: "string",
            }
        },
        {
            name: schemaName.BrowserSearchLogs,
            primaryKey: '_id',
            properties: {
                _id: 'string',
                url: "string",
                time: "int",
                title: { type: 'string', default: "", optional: true },
                icon: { type: 'string', default: "", optional: true },
                collect: { type: 'bool', default: false, optional: true },
            }
        },
        {
            name: schemaName.BrowserPopularLogs,
            primaryKey: '_id',
            properties: {
                _id: 'string',
                url: "string",
                time: "int",
                title: { type: 'string', default: "", optional: true },
                icon: { type: 'string', default: "", optional: true },
                collect: { type: 'bool', default: false, optional: true },
                chainId: { type: 'int', default: 1 },
            }
        }
    ]
}


export const realm = schema

export const closeRealm = () => {
    for (let i in schemaName) {
        realm.write(() => {
            realm.delete(realm.objects(schemaName[i]));
        })
    }
}
